package com.zh.work.service.platform.branch.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.branch.BranchInfoMapper;
import com.zh.work.entity.branch.BranchInfoEntity;
import com.zh.work.entity.branch.QrcodeBranchFee;
import com.zh.work.entity.branch.SystemBank;
import com.zh.work.service.platform.branch.BranchInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: Info设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/08/23
 * @Version: 1.0.1
 */
@Service
public class BranchInfoServiceImpl extends ServiceImpl<BranchInfoMapper, BranchInfoEntity> implements BranchInfoService {

    private static final Logger log = LoggerFactory.getLogger(BranchInfoServiceImpl.class);

    @Autowired
    private BranchInfoMapper branchInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Qualifier("taskExecutor")
    @Autowired
    private TaskExecutor taskExecutor;

    @Override
    public List<QrcodeBranchFee> getQrCodeRate(String branchNo) { return branchInfoMapper.getQrCodeRate(branchNo); }



    @Override
    @Transactional
    public Map<String, Object> getBankList(String searchType, String bankName,String bankCode,String province,String city) {
        Map<String,Object> map = new HashMap<>();
        if (searchType.equals("1")) {
            // 获取地区选择项
            Map<String, List<SystemBank>> stringListMap = (Map<String, List<SystemBank>>) this.redisTemplate.opsForValue().get("AREA_KEY");
            // 获取银行类别
            List<BranchInfoEntity> list = (List<BranchInfoEntity>) this.redisTemplate.opsForValue().get("BRANCH_TYPE");
            if (stringListMap == null)
                this.getProvinceAndCity();
            if (list == null)
                this.getBranchType();
            List<String> stringList = (List<String>) this.redisTemplate.opsForValue().get("AREA_PROVINCE");
            map.put("AREA", stringListMap);
            map.put("AREA_PROVINCE", stringList);
            map.put("BRANCH_TYPE", list);
        } else {
            List<SystemBank> list = this.branchInfoMapper.getBankList(bankName, bankCode, province, city);
            map.put("list", list);
        }
        return map;
    }

    @Override
    public int addQrBrcFee(List<QrcodeBranchFee> qrcodeBranchFees) { return this.branchInfoMapper.addQrBrcFee(qrcodeBranchFees); }

    @Override
    public int delQrBrcFee(String branchNo) { return this.branchInfoMapper.delQrBrcFee(branchNo); }

    @Override
    public void getProvinceAndCity() {
        List<SystemBank> list = branchInfoMapper.getProvinceAndCity();
        List<String> stringArr = new ArrayList<>();
        Map<String, List<SystemBank>> stringListMap = list.stream().collect(Collectors.groupingBy(SystemBank::getProvince));
        stringListMap.forEach( (m, systemBanks) -> { stringArr.add(m); });
        this.redisTemplate.opsForValue().set("AREA_KEY", stringListMap);
        this.redisTemplate.opsForValue().set("AREA_PROVINCE", stringArr);
    }


    @Override
    public void getBranchType() {
        List<BranchInfoEntity> list = branchInfoMapper.getBranchType();
        this.redisTemplate.opsForValue().set("BRANCH_TYPE", list);
    }

    @Override
    public Map<String,Object> getRoleBindList(HashMap<String,String> map) {
        List<List<?>> list = branchInfoMapper.getRoleBindList(map);
        Map<String,Object> hashMap = new HashMap();
        if (list.size() == 3) {
            hashMap.put("branch", list.get(0));
            hashMap.put("agent", list.get(1));
            hashMap.put("merchant", list.get(2));
        }
        return hashMap;
    }

}
